package net.jsoft.platform.activiti.explorer;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
//import org.springframework.context.annotation.Bean;
//import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.apache.logging.log4j.Logger;

public class JsonpCallbackFilter implements Filter
{
    private static final Logger logger = LogManager.getLogger();

    public void init(FilterConfig fConfig) throws ServletException
    {
    }

    @SuppressWarnings("rawtypes")
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
    {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;

        Map parms = httpRequest.getParameterMap();

        if (parms.containsKey("callback"))
        {
            if (logger.isDebugEnabled())
            {
                logger.debug("Wrapping response with JSONP callback '" + ((String[]) parms.get("callback"))[0] + "'");

            }
            OutputStream out = httpResponse.getOutputStream();

            GenericResponseWrapper wrapper = new GenericResponseWrapper(httpResponse);

            chain.doFilter(request, wrapper);

            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            outputStream.write(new String(((String[]) parms.get("callback"))[0] + "(").getBytes());
            outputStream.write(wrapper.getData());
            outputStream.write(new String(");").getBytes());
            byte[] jsonpResponse = outputStream.toByteArray();

            wrapper.setContentType("text/javascript;charset=UTF-8");
            wrapper.setContentLength(jsonpResponse.length);

            out.write(jsonpResponse);

            out.close();
        }
        else
        {
            chain.doFilter(request, response);
        }
    }

    public void destroy()
    {
    }
}
